package priv.dajie.tzzq.bot.model;

import java.util.ArrayList;
import java.util.Date;

public class HistoryPrices extends ArrayList<HistoryPrice>{
//    List<HistoryPrice> data;

//    public HistoryPrices(List<HistoryPrice> data) {
//        this.data = data;
//    }

    public double getAvgPrice(long day) {
       double sum = 0;
       int effective = 0;
       Date date = new Date();
       long time = date.getTime() - (date.getTime() % (60*60*24*1000)) - (60*60*8*1000);
       for(int i=size()-1;i>=0;i--){
            HistoryPrice historyPrice = get(i);
            if(time - historyPrice.getDate().getTime() < (60*60*24*1000*day)) {
                if(historyPrice.getPrice().doubleValue()!=0) {
                    sum += (historyPrice.getPrice().doubleValue() * historyPrice.getNum());
                    effective+=historyPrice.getNum();
                }
            }else{
                break;
            }
       }
       if(sum == 0) {
           return 0;
       }
       return sum/effective;
   }

   public int effective(long day){
       int effective = 0;
       Date date = new Date();
       long time = date.getTime() - (date.getTime() % (60*60*24*1000)) - (60*60*8*1000);
       for(int i=size()-1;i>=0;i--){
           HistoryPrice historyPrice = get(i);
           long gap = time - historyPrice.getDate().getTime();
//           System.out.println(historyPrice.getDate());
//           System.out.println(gap +" < "+  (60*60*24*1000*day) +" : "+ (gap < (60*60*24*1000*day)));
           if(gap < (60*60*24*1000*day)) {
               if(historyPrice.getPrice().doubleValue()!=0) {
                   effective += historyPrice.getNum();
               }
           }else{
               break;
           }
       }
       return effective;
   }

   public double deviationRate(long day){
        double avg = getAvgPrice(day);
        int effective = 0;
        Date date = new Date();
        long time = date.getTime() - (date.getTime() % (60*60*24*1000)) - (60*60*8*1000);
        double deviation = 0;
        for(int i=size()-1;i>=0;i--){
            HistoryPrice historyPrice = get(i);
            long gap = time - historyPrice.getDate().getTime();
    //           System.out.println(historyPrice.getDate());
            if(gap < (60*60*24*1000*day)) {
                if(historyPrice.getPrice().doubleValue()!=0) {
                    deviation += (Math.abs(historyPrice.getPrice().doubleValue() - avg)/avg)*historyPrice.getNum();
                    effective += historyPrice.getNum();
                }
            }else{
                break;
            }
       }
        return deviation/effective;
    }


   public HistoryPrice get(Date date){
       for(HistoryPrice historyPrice : this) {
           if (historyPrice.getDate().equals(date)) {
               return historyPrice;
           }
       }
       return null;
   }


}
